﻿using System;
using System.Collections.Generic;
using System.IO;

namespace Daq.Tools.HttpClientHelper
{
    /// <summary>
    /// Post表单提交参数
    /// </summary>
    public class DaqMultipartRequest
    {
        /// <summary>
        /// 请求头
        /// </summary>
        public Dictionary<string, string> HeadersKv { get; private set; } = new Dictionary<string, string>();

        /// <summary>
        /// 发送的文件
        /// key：要添加的 HTTP 内容的名称。T1：文件名,T2：文件流
        /// </summary>
        public Dictionary<string, (string, Stream)> FilesKv { get; private set; } = new Dictionary<string, (string, Stream)>();

        /// <summary>
        /// 请求的参数
        /// </summary>
        public Dictionary<string, string> ParamsKv { get; private set; } = new Dictionary<string, string>();

        /// <summary>
        /// 请求耗时时间秒
        /// </summary>
        public int TimeoutSecond { get; private set; } = 100;

        /// <summary>
        /// 添加请求超时前等待时间
        /// </summary>
        /// <param name="Timeout">秒，默认100秒</param>
        /// <returns></returns>
        public DaqMultipartRequest AddTimeout(int Timeout)
        {
            TimeoutSecond = Timeout;
            return this;
        }

        /// <summary>
        /// 添加请求头
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public DaqMultipartRequest AddHeader(string name, string value)
        {
            HeadersKv.Add(name, value);
            return this;
        }

        /// <summary>
        /// 添加请求参数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public DaqMultipartRequest AddParam(string name, string value)
        {
            ParamsKv.Add(name, value);
            return this;
        }

        /// <summary>
        /// 添加请求参数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public DaqMultipartRequest AddParam(string name, object value)
        {
            ParamsKv.Add(name, value.ToJson());
            return this;
        }

        /// <summary>
        /// 添加多个请求参数
        /// </summary>
        /// <param name="obj">可以是new {a = 1, b = 2}，也可以是Dictionary<string, string></param>
        /// <returns></returns>
        public DaqMultipartRequest AddParams(object obj)
        {
            var keyValues = obj.ToJson().ToObject<Dictionary<string, string>>();
            foreach (var kv in keyValues)
            {
                ParamsKv.Add(kv.Key, kv.Value);
            }
            return this;
        }

        /// <summary>
        /// 添加文件
        /// </summary>
        /// <param name="name"> 要添加的 HTTP 内容的名称。</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public DaqMultipartRequest AddFile(string name, string filePath)
        {
            using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                var transfer = new MemoryStream();
                fs.Seek(0, SeekOrigin.Begin);
                fs.CopyTo(transfer);
                fs.Flush();
                transfer.Seek(0, SeekOrigin.Begin);
                FilesKv.Add(name, (Path.GetFileName(filePath), transfer));
                return this;
            }
        }

        /// <summary>
        /// 添加文件
        /// </summary>
        /// <param name="name"> 要添加的 HTTP 内容的名称。</param>
        /// <param name="stream">文件流</param>
        /// <param name="fileName">要添加到集合中的 HTTP 内容的文件名。</param>
        /// <returns></returns>
        public DaqMultipartRequest AddFile(string name, Stream stream, string fileName)
        {
            stream.Seek(0, SeekOrigin.Begin);
            FilesKv.Add(name, (fileName, stream));
            return this;
        }
    }
}